﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using LitJson;
using UnityEditor;


public class InputData
{
    public Dictionary<string, List<int>> inputDataDict = new Dictionary<string, List<int>>();
}


public class EmBattleUI : MonoBehaviour {

    public List<InputField> myInput = new List<InputField>();
    public List<InputField> enemyInput = new List<InputField>();
    public FishGenerate fg;
    public BuffGenerate bg;

    private List<int> myHero = new List<int>();
    private List<int> myStane = new List<int>();
    private List<int> enemyHero = new List<int>();
    private List<int> enemyStane = new List<int>();

    private InputData dict = new InputData();

    // Use this for initialization
    void Start () {

        fg = GameControl._instance.fg;
        bg = GameControl._instance.bg;
        InitInputFiled();
    }
	
    //初始化输入
    public void InitInputFiled()
    {
        LoadJson();

        for (int i = 0; i < myInput.Count; i++)
        {
            for(int j = 0;j < myStane.Count; j++)
            {
                if (i + 1 == myStane[j])
                {
                    myInput[i].text = myHero[j].ToString();
                    break;
                }
                else
                {
                    myInput[i].text = (i + 1).ToString();  
                }
            }
        }
        for (int i = 0; i < enemyInput.Count; i++)
        {
            for (int j = 0; j < enemyStane.Count; j++)
            {
                if (i + 1 == enemyStane[j])
                {
                    enemyInput[i].text = enemyHero[j].ToString();
                    break;
                }
                else
                {
                    enemyInput[i].text = (i + 1).ToString();
                }
            }
        }
        
    }

    //刷新重新生成鱼，不要在战斗过程中点击
    public void GenetateEmBattle()
    {
        if (fg.DictFish != null) fg.DictFish.Clear();
        if (bg.buffDict != null) bg.buffDict.Clear();

        GameControl._instance.InitFishDict();

        //清理之前的问题
        GameControl._instance.myHero.Clear();
        GameControl._instance.myStane.Clear();
        GameControl._instance.EnemyHero.Clear();
        GameControl._instance.EnemyStance.Clear();
        myHero.Clear();
        myStane.Clear();
        enemyHero.Clear();
        enemyStane.Clear();

        //生成ID
        for (int i = 0; i < myInput.Count; i++)
        { 
            if (fg.DictFish.ContainsKey(int.Parse(myInput[i].text)))
            {
                GameControl._instance.myHero.Add(int.Parse(myInput[i].text));
                GameControl._instance.myStane.Add(i+1);
                myHero.Add(int.Parse(myInput[i].text));
                myStane.Add(i + 1);
                //print(myInput[i].text + "   " + (i + 1));
            }
            else
            {
                Debug.LogWarning("没有这个ID");
            }
        }
        for (int i = 0; i < enemyInput.Count; i++)
        {
            if (fg.DictFish.ContainsKey(int.Parse(enemyInput[i].text)))
            {
                GameControl._instance.EnemyHero.Add(int.Parse(enemyInput[i].text));
                GameControl._instance.EnemyStance.Add(i + 1);
                enemyHero.Add(int.Parse(enemyInput[i].text));
                enemyStane.Add(i + 1);
                //print(enemyInput[i].text + "   " + (i + 1));
            }
            else
            {
                Debug.LogWarning("没有这个ID");
            }
        }
        SaveAsJson();
        GameControl._instance.InitFish();
    }

    /// <summary>
    /// 你保存Json到本地的方法
    /// </summary>
    public void SaveAsJson()
    {
        string filePath = Application.dataPath + @"/Resources/JsonArray.json";
        //Debug.Log(Application.dataPath + @"/Resources/JsonArray.json");
        

        if (!File.Exists(filePath))//不存在就创建键值对
        { 
            dict.inputDataDict.Add("myHero", myHero);
            dict.inputDataDict.Add("myStane", myStane);
            dict.inputDataDict.Add("enemyHero", enemyHero);
            dict.inputDataDict.Add("enemyStane", enemyStane);
        }
        else//若存在就更新值
        {
            dict.inputDataDict["myHero"] = myHero;
            dict.inputDataDict["myStane"] = myStane;
            dict.inputDataDict["enemyHero"] = enemyHero;
            dict.inputDataDict["enemyStane"] = enemyStane;
        }

        //找到当前路径
        FileInfo file = new FileInfo(filePath);
        //判断有没有文件，有则打开文件，没有创建后打开文件
        StreamWriter sw = file.CreateText();
        //ToJson接口将你的列表类穿进去，并自动转化为string类型
        string json = JsonMapper.ToJson(dict.inputDataDict);
        //将转好的字符串存进文件
        sw.WriteLine(json);
        //释放资源
        sw.Close();
        sw.Dispose();

        AssetDatabase.Refresh();
    }

    /// <summary>
    /// 读取保存数据的方法
    /// </summary>
    public void LoadJson()
    {
        TextAsset asset = Resources.Load("JsonArray") as TextAsset;
        if (!asset)
        {
            Debug.Log("没有任何数据");
            return;
        }

        string strdata = asset.text;
        JsonData jsdata = JsonMapper.ToObject(strdata);

        for(int i = 0; i < jsdata["myHero"].Count; i++)
        {
            string j = jsdata["myHero"][i].ToString();
            myHero.Add(int.Parse(j));
        }
        for (int i = 0; i < jsdata["myStane"].Count; i++)
        {
            string j = jsdata["myStane"][i].ToString();
            myStane.Add(int.Parse(j));
        }
        for (int i = 0; i < jsdata["enemyHero"].Count; i++)
        {
            string j = jsdata["enemyHero"][i].ToString();
            enemyHero.Add(int.Parse(j));
        }
        for (int i = 0; i < jsdata["enemyStane"].Count; i++)
        {
            string j = jsdata["enemyStane"][i].ToString();
            enemyStane.Add(int.Parse(j));
        }
    }
}
